/**
 * Copyright (c) 2011 Nokia Corporation.
 */

import QtQuick 1.0
import com.nokia.symbian 1.0
import "SymbianConstants.js" as Constants
import "."

Page {
    id: sensorListPage
    orientationLock: root.orientation

    SensorReadingPage {
        id: sensorReadingPage
        orientationLock: root.orientation
    }

    ListView {
        id: sensorListView
        anchors.fill: parent
        model: sensorModel

        delegate: ListItem {
            id: listItem
            subItemIndicator: true

            Column {
                anchors.fill: listItem.paddingItem

                ListItemText {
                    mode: listItem.mode
                    role: "Title"
                    text: type
                }
            }

            onClicked: {
                var success = sensorManager.setSensorType(type);

                if (success) {
                    pageStack.push(sensorReadingPage);
                }
                else {
                    console.debug("Failed to set the sensor!");
                    errorDialog.titleText = "Failed to start sensor!";
                    errorDialog.contentText =
                        "This could indicate that the sensor is not " +
                        "supported by the device.";
                    errorDialog.open();
                    hideTimer.restart();
                }
            }
        }
    }

    ScrollDecorator {
        flickableItem: sensorListView
    }

    Dialog {
        id: errorDialog
        property alias titleText: titleItem.text
        property alias contentText: contentItem.text
        width: parent.width - Constants.DEFAULT_MARGIN

        title: Item {
            x: Constants.DEFAULT_MARGIN
            height: parent.height

            Text {
                id: titleItem
                height: parent.height
                color: "white"
                font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
                verticalAlignment: Text.AlignVCenter
            }
        }

        content: Item {
            Text {
                id: contentItem
                x: Constants.DEFAULT_MARGIN
                width: errorDialog.width - Constants.DEFAULT_MARGIN * 2
                color: "#eeeeee"
                font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
                wrapMode: Text.WordWrap
            }
        }

        Timer {
            id: hideTimer
            interval: 3000
            repeat: false
            onTriggered: errorDialog.close();
        }
    }

    Dialog {
        id: infoDialog
        visualParent: sensorListPage
        width: parent.width - Constants.DEFAULT_MARGIN

        title: Item {
            x: Constants.DEFAULT_MARGIN
            height: parent.height

            Text {
                height: parent.height
                color: "white"
                font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
                verticalAlignment: Text.AlignVCenter
                text: "Sensor Gallery version 2.0"
            }
        }

        content: Item {
            Text {
                x: Constants.DEFAULT_MARGIN
                width: infoDialog.width - Constants.DEFAULT_MARGIN * 2
                z: 1
                color: "#eeeeee"
                wrapMode: Text.WordWrap
                textFormat: Text.RichText
                text: "Example application using Qt Mobility to access " +
                      "sensors readings. For more information visit <a href=\"" +
                      "http://projects.developer.nokia.com/sensorgallery\">" +
                      "projects.developer.nokia.com</a>.";
                onLinkActivated: Qt.openUrlExternally(link)
            }
        }

        buttons: Button {
            x: infoDialog.width / 2 - width / 2
            text: "Close"
            onClicked: infoDialog.close();
        }
    }

    // Orientation setting selection menu
    Menu {
        id: orientationMenu
        visualParent: pageStack

        MenuLayout {
            MenuItem {
                text: "Auto orientation";
                onClicked: root.orientation = PageOrientation.Automatic;
            }
            MenuItem {
                text: "Lock to portrait";
                onClicked: root.orientation = PageOrientation.LockPortrait;
            }
            MenuItem {
                text: "Lock to landscape";
                onClicked: root.orientation = PageOrientation.LockLandscape;
            }
            MenuItem {
                text: "About";
                onClicked: infoDialog.open();
            }

        }
    }

    tools: ToolBarLayout {
        ToolButton {
            iconSource: "toolbar-back";
            onClicked: Qt.quit();
        }
        ToolButton {
            iconSource: "toolbar-menu";
            onClicked: (orientationMenu.status == DialogStatus.Closed) ?
                           orientationMenu.open() : orientationMenu.close();
        }
    }
}

// End of file.
